package com.hillinsight.config;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfig {

//    @Autowired
//    private Environment env;

    @Bean(name = "mysqlDataSource")
    @Primary
    @ConfigurationProperties(prefix="mysql.spring.datasource")
    public DataSource mysqlDataSource() throws Exception{
//        DriverManagerDataSource dataSource = new DriverManagerDataSource();
//        dataSource.setDriverClassName(env.getProperty("mysql.spring.datasource.driver-class-name"));
//        dataSource.setUrl(env.getProperty("mysql.spring.datasource.url"));
//        dataSource.setUsername(env.getProperty("mysql.spring.datasource.username"));
//        dataSource.setPassword(env.getProperty("mysql.spring.datasource.password"));
//        return dataSource;
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "hiveDataSource")
    @ConfigurationProperties(prefix="hive.spring.datasource")
    public DataSource hiveDataSource()  throws Exception{
//        DriverManagerDataSource dataSource = new DriverManagerDataSource();
//        dataSource.setDriverClassName(env.getProperty("hive.spring.datasource.driver-class-name"));
//        dataSource.setUrl(env.getProperty("hive.spring.datasource.url"));
//        dataSource.setUsername(env.getProperty("hive.spring.datasource.username"));
//        dataSource.setPassword(env.getProperty("hive.spring.datasource.password"));
//        return dataSource;
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "mysqlJdbcTemplate")
    @Primary
    public JdbcTemplate mysqlJdbcTemplate(
            @Qualifier("mysqlDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Bean(name = "hiveJdbcTemplate")
    public JdbcTemplate hiveJdbcTemplate(
            @Qualifier("hiveDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}